package mobmap.tela;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import mobmap.conf.Config;
import mobmap.util.Log;
import mobmap.util.MapUtil;
import mobmap.util.Quadro;
import mobmap.util.imagecache.Cache;


public class MapCanvas extends Canvas {

	private static final int ANCORA = Graphics.TOP | Graphics.LEFT;
	private static final int STEP = 10;

	Cache cache;
	int posVertical;
	int posHorizontal;
	
	//se mantido botao pressionado
	boolean keyHold = false;
	
	private boolean enableMiniMap = false;

	MapUtil mapa;

	public MapCanvas() {

		// inicia na pos proximo ao meio do mapa
		posVertical = -((Config.MAP_HEIGHT  / Quadro.SIZE) /  2) * Quadro.SIZE;
		posHorizontal = -((Config.MAP_WIDTH  / Quadro.SIZE) /  2) * Quadro.SIZE;

		cache = new Cache();
		mapa = new MapUtil(posHorizontal, posVertical);
	}

	public void paint(Graphics tela) {

		Log.out("[PontoH: " + posHorizontal);
		Log.out(" | PontoV: " + posVertical + "]");

		tela.setColor(255, 255, 255);
		tela.fillRect(0, 0, getWidth(), getHeight());

		mapa.setPosHorizontal(posHorizontal);
		mapa.setPosVertical(posVertical);

		Quadro[][] imgs = mapa.getImagensRelativas();

		Log.out("-------------------");
		printImage(tela, imgs[0][0]);
		printImage(tela, imgs[0][1]);
		printImage(tela, imgs[1][0]);
		printImage(tela, imgs[1][1]);

		if (enableMiniMap)
			drawMiniMap(tela);
		
		
		System.gc();

		Log.out("----- MEMORY ------");
		Log.out("[Total: " + Runtime.getRuntime().totalMemory()
				/ 8192 + "KB]");
		Log.out("[Free: " + Runtime.getRuntime().freeMemory() / 8192
				+ "KB]");

	}

	private void printImage(Graphics g, Quadro quadro) {
		try {
			g.drawImage(getImage(quadro), quadro.getPosHRelativa(), quadro
					.getPosVRelativa(), ANCORA);
		} catch (IOException e) {
			Log.out("NÃO FOI POSSÍVEL RENDERIZAR A IMG "
					+ e.getMessage());
		}
	}

	protected Image getImage(Quadro quadro) throws IOException {
		Log.out("Carregando Imagem: /mapa/" + quadro.getNomeImagem());

		// com cache image desativado
		// return Image.createImage("/mapa/" + ir.getNomeImagem());

		// com cache image ativado
		return cache.getImage("/rsc/mapa/" + quadro.getNomeImagem());
	}
	
	protected Image getImage(String nome) throws IOException {
		Log.out("Carregando Imagem: /mapa/" + nome);
		return cache.getImage("/rsc/mapa/" + nome);
	}


	protected void keyPressed(int keyCode) {
			moveTo(keyCode);
	}

	private void moveTo(int keyCode) {
		
		
		int gameCode = getGameAction(keyCode);

		// se for uma tecla game
		if (gameCode != 0) {

			switch (gameCode) {
			case DOWN:
				posVertical = posVertical - STEP;
				break;
			case UP:
				posVertical = posVertical + STEP;
				break;
			case LEFT:
				posHorizontal = posHorizontal + STEP;
				break;
			case RIGHT:
				posHorizontal = posHorizontal - STEP;
				break;
			}
		} else {
			// teclado numerico com passo valendo dobro
			switch (keyCode) {
			case KEY_NUM8:
				posVertical = posVertical - STEP * 4;
				break;
			case KEY_NUM2:
				posVertical = posVertical + STEP * 4;
				break;
			case KEY_NUM4:
				posHorizontal = posHorizontal + STEP * 4;
				break;
			case KEY_NUM6:
				posHorizontal = posHorizontal - STEP * 4;
				break;
			}
		}

		repaint();
		
	}

	
	private void drawMiniMap(Graphics g){
		
		//tamanho do thumb do mapa
		double thubSizeD = 150.0;
		int thubSizeI = (int)thubSizeD;
 		
		try{
		g.drawImage(Image.createImage("/rsc/mapa/thumb.png"), getWidth() - thubSizeI, 
				getHeight() - thubSizeI, Graphics.TOP | Graphics.LEFT);
		
		g.setColor(0,0,0);
		g.drawRect(getWidth() - thubSizeI, 
				getHeight() - thubSizeI, thubSizeI, thubSizeI);
		}catch (Exception e) {
			g.fillRect(getWidth() - thubSizeI, 
					getHeight() - thubSizeI, thubSizeI, thubSizeI);
		}
		
		double percV = thubSizeD / Config.MAP_HEIGHT;
		double percH = thubSizeD / Config.MAP_WIDTH;
		
		double xD = -1 * (posHorizontal * percH);
		double yD = -1 * (posVertical * percV);
		int x = new Double(xD).intValue() + (getWidth() - thubSizeI);
		int y = new Double(yD).intValue() + (getHeight() - thubSizeI);

		g.setColor(100, 100, 100);
		g.drawRect(x , y, 10, 13);

	}

	public boolean isEnableMiniMap() {
		return enableMiniMap;
	}

	public void setEnableMiniMap(boolean showMiniMap) {
		this.enableMiniMap = showMiniMap;
		repaint();
	}
	
}
class Mover implements Runnable{
	int key;
	
	public Mover(int keyPressed){
		key = keyPressed;
	}
	
	public void run() {
		
		
	}
}